package classwork;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Client {
    private Socket socket;
    public Client(){
        try {
            System.out.println("服务器端连接中...");
             socket = new Socket("Localhost",8777);
            System.out.println("服务器端连接成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public void start(){
        String name = "PXL";
        try {
            OutputStream out = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw,true);
            pw.println("我已黑入服务器");
            Scanner scan = new Scanner(System.in);
            while (true){

                String line = scan.nextLine();
                if (line.equals("exit")){
                    break;
                }
                    pw.println(name+":"+line);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
